<?php

class KitBodyModel
{
    public function addKitBody($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']))
        {
            AC::$errorNo    =   'KBM001';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data['uid']        =   $kitInfo['uid'];
        $data['saveTime']   =   AC::SERVER_TIME;

        isset($kitInfo['content'])      &&  $data['content']    =   $kitInfo['content'];
        isset($kitInfo['category'])     &&  $data['category']   =   abs($kitInfo['category']);
        isset($kitInfo['secret'])       &&  $data['secret']     =   abs($kitInfo['secret']);
        isset($kitInfo['weather'])      &&  $data['weather']    =   abs($kitInfo['weather']);
        isset($kitInfo['emotion'])      &&  $data['emotion']    =   abs($kitInfo['emotion']);
        isset($kitInfo['textSize'])     &&  $data['textSize']   =   abs($kitInfo['textSize']);
        isset($kitInfo['textColor'])    &&  $data['textColor']  =   $kitInfo['textColor'];
        isset($kitInfo['theme'])        &&  $data['theme']      =   abs($kitInfo['theme']);
        isset($kitInfo['posX'])         &&  $data['posX']       =   $kitInfo['posX'];
        isset($kitInfo['posY'])         &&  $data['posY']       =   $kitInfo['posY'];
        isset($kitInfo['place'])        &&  $data['place']      =   $kitInfo['place'];

        $result =   Storage::addData($key, $data);
        if (empty($result))
        {
            AC::$errorNo    =   'KBM002';
            return  FALSE;
        }
        return  $result;
    }/*}}}*/

    public function getBodyId($kitInfo, $kitId)/*{{{*/
    {
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $bodyId =   UK::getBodyidByItemid($key, $kitId);

        if (empty($bodyId))
        {
            AC::$errorNo    =   'KBM003';
            return  FALSE;
        }
        return  $bodyId;
    }/*}}}*/

    public function getKitId($bodyId)/*{{{*/
    {
        $info   =   UK::getItemidByBodyid($bodyId);
        if (empty($info['itemId']))
        {
            AC::$errorNo    =   'KBM005';
            return  FALSE;
        }
        return  $info['itemId'];
    }/*}}}*/

    public function getKitBody($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid'])  ||  empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KBM004';
            return  FALSE;
        }
        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $kitId  =   $this->getKitId($kitInfo['bodyId']);
        if (empty($kitId))    return  FALSE;

        $where  =   array('id' => $kitId, 'uid' => $kitInfo['uid']);
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }/*}}}*/

    public function setKitBody($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KBM008';
            return  FALSE;
        }
        $kitId  =   $this->getKitId($kitInfo['bodyId']);
        if (empty($kitId))  return  FALSE;

        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data['saveTime']   =   AC::SERVER_TIME;

        isset($kitInfo['content'])      &&  $data['content']    =   $kitInfo['content'];
        isset($kitInfo['category'])     &&  $data['category']   =   $kitInfo['category'];
        isset($kitInfo['secret'])       &&  $data['secret']     =   $kitInfo['secret'];
        isset($kitInfo['weather'])      &&  $data['weather']    =   $kitInfo['weather'];
        isset($kitInfo['emotion'])      &&  $data['emotion']    =   $kitInfo['emotion'];
        isset($kitInfo['textSize'])     &&  $data['textSize']   =   $kitInfo['textSize'];
        isset($kitInfo['textColor'])    &&  $data['textColor']  =   $kitInfo['textColor'];
        isset($kitInfo['theme'])        &&  $data['theme']      =   $kitInfo['theme'];
        isset($kitInfo['posX'])         &&  $data['posX']       =   $kitInfo['posX'];
        isset($kitInfo['posY'])         &&  $data['posY']       =   $kitInfo['posY'];
        isset($kitInfo['place'])        &&  $data['place']      =   $kitInfo['place'];

        $where  =   array('id' => $kitId);
        $result =   Storage::setConditionData($key, $data, $where);

        if (empty($result))
        {
            AC::$errorNo    =   'KBM009';
            return  FALSE;
        }
        return  $result;
    }/*}}}*/

    public function removeKitBody($kitInfo)/*{{{*/
    {
        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KBM006';
            return  FALSE;
        }
        $kitId  =   $this->getKitId($kitInfo['bodyId']);
        if (empty($kitId))    return  FALSE;

        $key    =   $this->getKitKeyPrefix($kitInfo['kitType'], $kitInfo['uid']);
        if (empty($key))    return  FALSE;

        $data   =   array('status' => 1, 'saveTime' => AC::SERVER_TIME);
        $where  =   array('id' => $kitId);
        $result =   Storage::setConditionData($key, $data, $where);
        if (empty($result))
        {
            AC::$errorNo    =   'KBM007';
        }
        return  $result;
    }/*}}}*/

    private function getKitKeyPrefix($kitType, $id)/*{{{*/
    {
        $kitTypeArray   =   array(
                                AC::FEED_KIT_ANNIVERSARY    =>  'KAB/',
                                AC::FEED_KIT_BMI            =>  'KBB/',
                                AC::FEED_KIT_MENSES         =>  'KMB/',
                                AC::FEED_KIT_NOTE           =>  'KNB/',
                                AC::FEED_KIT_SCHEDULE       =>  'KSB/',
                                AC::FEED_KIT_TALLYBOOK      =>  'KTB/',
                            );

        if (empty($kitTypeArray[$kitType]))
        {
            AC::$errorNo    =   'KBM010';
            return  FALSE;
        }
        $key    =   $kitTypeArray[$kitType]. $id;
        return  $key;
    }/*}}}*/

}


